package com.pencilisland.pay.mgt.console.controller;

import java.util.HashMap;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import com.pencilisland.pay.mgt.console.bean.JstreeNode;
import com.pencilisland.pay.mgt.console.entity.SysOrg;
import com.pencilisland.pay.mgt.console.service.OrgService;

/**
 * 组织机构管理处理器
 * 
 * @author Breeze
 *
 */
@Controller
@RequestMapping(value = "/console/org")
public class OrgController {

	@Autowired
	OrgService orgService;

	@RequestMapping(value = "")
	public String orgTree() {
		return "console/org";
	}

	/**
	 * 组织机构树
	 * 
	 * @param rootId
	 * @return
	 */
	@RequestMapping(value = "/tree/data")
	@ResponseBody
	public JstreeNode orgTreeData(@RequestParam(value = "rootId", required = false, defaultValue = "1") Long rootId) {
		return orgService.orgTreeData(rootId);
	}
	
	@RequestMapping(value = "/tree/data/user")
	@ResponseBody
	public JstreeNode orgTreeDataUser(@RequestParam(value = "rootId", required = false, defaultValue = "1") Long rootId) {
		return orgService.orgTreeDataUser(rootId);
	}
	
	@RequestMapping(value = "/tree/data/user1")
	@ResponseBody
	public JstreeNode orgTreeDataUser1(@RequestParam(value = "rootId", required = false, defaultValue = "1") Long rootId) {
		return orgService.orgTreeDataUser1(rootId);
	}

	/**
	 * 保存更新
	 * 
	 * @param org
	 * @return
	 */
	@RequestMapping(value = "/save")
	@ResponseBody
	public SysOrg save(@ModelAttribute SysOrg org) {
		return orgService.save(org);
	}

	/**
	 * 加载数据
	 * 
	 * @param id
	 * @return
	 */
	@RequestMapping(value = "/load/{id}")
	@ResponseBody
	public Map<String, Object> load(@PathVariable(value = "id") Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			resultMap.put("data", orgService.loadData(id));
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}

	@RequestMapping(value = "/delete/{id}")
	@ResponseBody
	public Map<String, Object> delete(@PathVariable(value = "id") Long id) {
		Map<String, Object> resultMap = new HashMap<String, Object>();
		resultMap.put("success", true);
		try {
			orgService.delete(id);
		} catch (Exception e) {
			e.printStackTrace();
			resultMap.put("success", false);
		}
		return resultMap;
	}
}
